﻿namespace EveKillboard.Core.Base
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Net;
    using Interfaces;
    using Services;

    public abstract class KillmailFeedProviderBase : IKillmailFeedServices
    {
        public abstract void Dispose();
        public abstract IEnumerable<IFeedKillmail> GetKillmails();
    }

    public interface IHttpKillmailFeedProvider
    {
        string Url { get; set; }
        Dictionary<int, string> Keys { get; set; }
    }

    public abstract class HttpKillmailFeedProviderBase : KillmailFeedProviderBase, IHttpKillmailFeedProvider, ITraceSource
    {
        protected internal const int ERROR_NOKILL = 119;

        private string _url;
        private Dictionary<int, string> _keys;

        public TraceSource Trace { get; set; }

        protected HttpKillmailFeedProviderBase(string url, Dictionary<int, string> keys)
        {
            if (url == null) throw new ArgumentNullException("url");
            if (keys == null) throw new ArgumentNullException("keys");
            _url = url;
            _keys = keys;
        }

        public virtual Dictionary<int, string> Keys
        {
            get { return _keys; }
            set { if (value != null) _keys = value; }
        }

        public virtual string Url
        {
            get { return _url; }
            set { if (value != null) _url = value; }
        }


        public virtual HttpWebRequest CreateRequest(string uri)
        {
            if (uri == null) throw new ArgumentNullException("uri");

            var request = (HttpWebRequest)WebRequest.Create(uri) ;
            request.KeepAlive = false;
            request.Timeout = 10000;
            return request;
        }

        public override void Dispose()
        {
            _url = null;
            _keys = null;

            GC.SuppressFinalize(this);
        }
    }
    
}